package org.brisling.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.brisling.common.ConstSysParam;
import org.brisling.common.util.LongUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 自定义流程业务查询基类，需要执行流程查询的业务可继承该类
 * @author : jackson wang
 * @Date   : 2015年6月8日 下午9:47:33
 * @version: 1.0
 * @param <Object> 泛型需定义的业务类实体，一般非<code>Entity</code>对象
 */
public abstract class AbstractWorkflowQueryService<T> extends AbstractBaseQueryService<T>{

	private final static String ACT_RU_EXECUTION = "act_ru_execution";
	
	private final static String ACT_RU_TASK = "act_ru_task";
	
	private final static String ACT_HI_PROCINST = "act_hi_procinst";
	
	private final static String ACT_HI_TASKINST = "act_hi_taskinst";
	
	
	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(AbstractWorkflowQueryService.class);
	
	/**
	 * 查找指定业务模块的执行中业务实体集合
	 * @param fullBizcode
	 * @return
	 */
	protected List<T> findRunningList(String fullBizcode){
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("fullBizcode",fullBizcode);
		map.put("state","running");
		return findBizByState(map);
	}
	
	
	/**
	 * 查找指定业务模块的执行中业务实体集合
	 * @param fullBizcode
	 * @return
	 */
	protected List<T> findFinishedList(String fullBizcode){
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("fullBizcode",fullBizcode);
		map.put("state","finished");
		return findBizByState(map);
	}
	/**
	 * 查找指定业务模块的已签收业务实体集合
	 * @param fullBizcode
	 * @return
	 */
	protected List<T> findAssigneeList(String fullBizcode,String assignee){
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("fullBizcode",fullBizcode);
		map.put("assignee",assignee);
		return findBizByState(map);
	}
	
	/**
	 * 查找指定业务模块当前用户可签收业务实体集合
	 * @param fullBizcode	业务模块完整编码
	 * @param GroupsOrUsers	用户组或用户
	 * @return
	 */
	protected List<T> findCandidateList(String fullBizcode,Map<String,String> GroupsOrUsers){
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("fullBizcode",fullBizcode);
		map.put("candidate",GroupsOrUsers);
		return findBizByState(map);
	}
	
	
	/**
	 * 
	 * @param map
	 * 		key:
	 * 			fullBizcode:完整业务实体编码
	 * 			state:	业务状态("running","assignee","candidate","finished","suspend","invalidate")
	 * 			assignee:签收人员
	 * 			candidate:HashMap<String,String> ,groups:可签收组,users:可签收用户
	 * @return
	 */
	protected List<T> findBizByState(Map<String,Object> map){
		
		//根据查询对象拼装sql语句
		String sql = " SELECT b.* ,t.name_ as flowState ,t.id_ as flowTaskId FROM %1$S B,%2$s E ,%3$s T ";
		if(map.containsKey("state")){
			String flowState = (String) map.get("state");
			switch(flowState){
			case "running":
				sql = String.format(sql,this.moduleName,ACT_RU_EXECUTION,ACT_RU_TASK);
				break;
			case "finished":
				sql = String.format(sql,this.moduleName,ACT_HI_PROCINST,ACT_HI_TASKINST);
				break;
			default:
				sql = String.format(sql,this.moduleName,ACT_RU_EXECUTION,ACT_RU_TASK);
			}
			
		}
		
		//拼装where条件
		String whereCorse =  " where b.flowInstId = e.proc_inst_id_ and e.proc_inst_id_= t.proc_inst_id_ ";
		
		String conditionWhere = getFlowCondition(map);
		
		if(conditionWhere!=null){
			sql = sql +whereCorse + conditionWhere;
		}
		
		//处理分页
		Integer _page = ConstSysParam.PAGE;		
		if(map.containsKey("page")){
			_page = LongUtil.getIntValue((String) map.get("page"), _page);
		}
		
		Integer _rows = ConstSysParam.ROWS;
		if(map.containsKey("rows")){
			_rows = LongUtil.getIntValue((String) map.get("rows"), _rows);
		}
		return super.getPagedList(_page, _rows, sql);
		
	}
	
	/**
	 * 处理流程查询条件
	 * @param map	流程业务条件map对象
	 * @return	
	 */
	private String getFlowCondition(Map<String,Object> map){
		
		String retVal = null;
		if((map!=null) && (!map.isEmpty())){
			if(map.containsKey("fullBizcode")){
				retVal = String.format(" e.business_key_ = '%1$s'", map.get("fullBizcode"));
			}
			
			if(map.containsKey("assignee")){
				String _assignee = String.format(" t.assignee_ = '%1$s'", map.get("assignee"));
				retVal = retVal == null?_assignee:(retVal +" and " +_assignee); 
			}
		}
		
		return retVal;
	}
	
	
}
